package com.csv;

import com.csvreader.CsvReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CsvValidateService {
    public static String validate(String path){
        String filePath = path;
        String alertMessage = "success";
        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();

        //Validate same group setting same source port
        try {
            CsvReader csvReader = new CsvReader(filePath);
            csvReader.readHeaders();
            while (csvReader.readRecord()){
                if (csvReader.get("Destination").contains("*")){
                    list.add(csvReader.get("NLB-Group"));
                }
            }
            System.out.println(list);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            CsvReader csvReader = new CsvReader(filePath);
            csvReader.readHeaders();
            while (csvReader.readRecord()){
                if (!list.contains(csvReader.get("NLB-Group"))){
                    list1.add(csvReader.get("NLB-Group").concat(csvReader.get("source port")));
                }
            }
            System.out.println(list1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (int i = 0; i<list1.size(); i++) {
            for (int j= i+1; j < list1.size(); j++) {
                if (list1.get(i).equals(list1.get(j))) {
                    alertMessage = "You are setting same source port in same group";
                    System.out.println("same source port"+list1.get(i));
                }
            }
        }

        return alertMessage;

    }
}
